package com.quanyan.travel.web;

import com.quanyan.api.APIResponse;
import com.quanyan.api.enums.ApiResponseEnum;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.travel.api.req.ReqActivity;
import com.quanyan.travel.api.req.ReqActivityDetail;
import com.quanyan.travel.api.req.ReqOutdoor;
import com.quanyan.travel.api.resp.RespActivityComment;
import com.quanyan.travel.api.resp.RespActivityUser;
import com.quanyan.travel.api.resp.RespOutdoorActivityDetail;
import com.quanyan.travel.service.TravelDetailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @author dongshenglin
 * @date 2016/5/27  19:00
 */
@RestController
@RequestMapping("/api/travel/detail")
public class TravelDetailController {
    @Autowired
    private TravelDetailService travelDetailService;
    private static Logger log = LoggerFactory.getLogger(TravelDetailController.class);

    /**
     * 查看活动详情列表
     * */
    @RequestMapping(value="/overview", method = RequestMethod.POST)
    public APIResponse<RespOutdoorActivityDetail> getOverview(@RequestBody Map map)  {
        if (StringUtils.isEmpty(map.get("activityId"))){
            return APIResponse.returnFail("活动id为空");
        }
        return APIResponse.returnSuccess(travelDetailService.getTravelOverviewSingle((Integer)map.get("activityId")));
    }


    /**
     * 查询所有活动收藏用户
     * @param reqOutdoor
     * @return
     */
    @RequestMapping(value = "/getAllCollectionUsers",method = RequestMethod.POST)
    public APIResponse<PageObj<List<RespActivityUser>>> getCollectionAndComment(@RequestBody @Valid ReqOutdoor reqOutdoor){
        if (StringUtils.isEmpty(reqOutdoor.getActivityId())){
            return APIResponse.returnFail("活动id为空");
        }
        return travelDetailService.getPageCollectionUsers(reqOutdoor.getActivityId());
    }

    /**
     * 获取六个最新的收藏用户
     */
    @RequestMapping(value = "/getSixCollectUser",method = RequestMethod.POST)

    public APIResponse<PageObj<List<RespActivityUser>>> getSixCollectUser(@RequestBody @Valid ReqOutdoor reqOutdoor){
        if (StringUtils.isEmpty(reqOutdoor.getActivityId())){
            return APIResponse.returnFail("活动id为空");
        }
        return travelDetailService.getSixCollectUser(reqOutdoor.getActivityId());
    }


    /**
     * 获取活动评论列表(分页对象)
     */
    @RequestMapping(value = "/getPageActivityComments",method = RequestMethod.POST)
    public APIResponse<PageObj<List<RespActivityComment>>> getActivityComments(@RequestBody @Valid ReqActivity request){
        return travelDetailService.getPageActivityComments(request);
    }

    /**
     * 用户收藏活动/取消收藏
     */
    @RequestMapping(value = "/changeFavorite",method = RequestMethod.POST)
    public APIResponse<Boolean> changeFavorite(@RequestBody @Valid ReqActivityDetail reqActivityDetail, HttpServletRequest request){
        if(reqActivityDetail.getChangeType() != 0 && reqActivityDetail.getChangeType()!= 1){
            APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        boolean result = travelDetailService.changeFavorite((Integer) request.getAttribute("uid"),reqActivityDetail.getActivityId(),reqActivityDetail.getChangeType());
        if (result){
            return APIResponse.returnSuccess(result);
        }else{
            return APIResponse.returnFail("服务器异常");
        }
    }

    /**
     * 价格日历
     */
    @RequestMapping(value = "/getPriceCalendar",method = RequestMethod.POST)
    public APIResponse<?> getCollectionUsers(@RequestBody @Valid ReqOutdoor reqOutdoor, HttpServletRequest request){
        if (StringUtils.isEmpty(reqOutdoor.getActivityId())){
            return APIResponse.returnFail("活动id为空");
        }
        return travelDetailService.getPriceCalendar((Integer) request.getAttribute("uid"), reqOutdoor.getActivityId(),request);
    }

}
